package com.example.study.service.impl;

import com.example.study.common.annotation.DataSource;
import com.example.study.entity.Student;
import com.example.study.repository.StudentRepository;
import com.example.study.service.IStudentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: liqiancheng
 * @Email: liqiancheng@centfor.com
 * @Date: 2020/6/4 15:46
 **/
@DataSource("second")
@Service
public class StudentServiceImpl implements IStudentService {

    @Resource
    private StudentRepository studentRepository;

    @Override
    public Student save(Student student) {
        return studentRepository.save(student);
    }

    @Override
    public Student update(Student student) {
        return studentRepository.save(student);
    }

    @Override
    public void delete(int id) {
        studentRepository.deleteById(id);
    }

    @Override
    public List<Student> List() {
        Iterator<Student> studentIterator = studentRepository.findAll().iterator();
        List<Student> studentList = new ArrayList<>();
        while (studentIterator.hasNext()) {
            studentList.add(studentIterator.next());
        }
        return studentList;
    }
}
